package classes.java;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletOne
 */
public class ServletOne extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletOne() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");//MIME type
		PrintWriter pw = response.getWriter();
		if(!(request.getParameter("session") != null && request.getParameter("session").equals("validSession"))){
			System.out.println("Not a valid session");			
			pw.println("Session is expired. Please Login again");
			
		}else{
		System.out.println("servie method");
		ServletConfig cg = getServletConfig();
		cg.getInitParameter("param1");
		System.out.println("init parameter"+ cg.getInitParameter("param1"));
		
		ServletContext context = getServletContext();
		System.out.println("context parameter"+ context.getInitParameter("contextParam1"));
		
		request.getParameter("userName");
		
		//response.setContentType("text/html");//MIME type
		//PrintWriter pw = response.getWriter();
		pw.println("<h1>This is my second application"+context.getInitParameter("contextParam1")+"</h1>");
		pw.println("user Name"+request.getParameter("userName"));
		pw.println("Password"+request.getParameter("password"));
		}
		System.out.println(request.getParameter("session"));
		pw.close();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("This is do post method");
	}

}
